//
//  ONEChatRegisterViewController.m
//  ONEChat
//
//  Created by 楼楚 on 15/12/16.
//  Copyright © 2015年 Chu Lou. All rights reserved.
//

#import "ONEChatRegisterViewController.h"
# import "AppDelegate.h"

@interface ONEChatRegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *registerUser;

@property (weak, nonatomic) IBOutlet UITextField *registerPwd;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;


@end

@implementation ONEChatRegisterViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
}
- (IBAction)registerClick {
    [self.view endEditing:YES];

    ONEChatUserInfo *userInfo = [ONEChatUserInfo sharedONEChatUserInfo];
    userInfo.registerUser = self.registerUser.text;
    userInfo.registerPwd = self.registerPwd.text;
    [ONEChatXMPPTool sharedONEChatXMPPTool].registerOperation = YES;
    __weak typeof(self) selfVc = self;
    [[ONEChatXMPPTool sharedONEChatXMPPTool] xmppUserRegister:^(XMPPResultType type) {
        
        
        [selfVc handleResultType:type];
    }];

}
- (void)handleResultType:(XMPPResultType)type
{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        switch (type) {
            case XMPPResultTypeRegisterSuccess:
                                    [MBProgressHUD showMessage:@"注册成功" toView:self.view];
                NSLog(@"注册成功");
                [self enterMainPage];                break;
                
            case XMPPResultTypeRegisterFailue:
                NSLog(@"注册失败");
                                    [MBProgressHUD showError:@"注册失败"toView:self.view ];
                break;
                
            case XMPPResultTypeNetErr:
                                    [MBProgressHUD showError:@"网络炸啦" toView:self.view ];
            default:
                NSLog(@"break");
                break;
        }
        
    });
}
- (void) enterMainPage{
    //    更改用户的登录状态为yes
    [ONEChatUserInfo sharedONEChatUserInfo].loginStatus = YES;
    //把用户登录成功的数据，保存到沙盒
    [[ONEChatUserInfo sharedONEChatUserInfo]saveUserInfoToSanbox];
    
    //隐藏模态窗口
    [self dismissViewControllerAnimated:NO completion:nil];
    //登录成功来到主界面
    [UIStoryboard showInitialVCWithName:@"Main"];
}

- (IBAction)cancel:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
